﻿using AIStudio.Wpf.AvalonDockMVVM.ViewModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace AIStudio.Wpf.HomePage.Helpers
{
    public static class AssemblyHelper
    {
        public static Type[] DockWindowViewModelTypes { get; private set; }
        public static Type[] AllTypes { get; private set; }
        static AssemblyHelper()
        {
             GetDockWindowViewModelType();
        }

        public static void GetDockWindowViewModelType()
        {
            var assemblies = System.AppDomain.CurrentDomain.GetAssemblies().Where(p => p.FullName.StartsWith("AIStudio.Wpf"));

            List<Type> dockWindowViewModelTypes = new List<Type>();
            List<Type> allTypes = new List<Type>();
            //List<Assembly> assemblies = new List<Assembly>();
            //var assembly = Assembly.GetExecutingAssembly();
            //assemblies.Add(assembly);

            //var arrReferencedAssmbNames = assembly.GetReferencedAssemblies();
            //foreach (AssemblyName assmblyName in arrReferencedAssmbNames.Where(p => p.FullName.StartsWith("AIStudio.Wpf")))
            //{
            //    assembly = Assembly.Load(assmblyName);
            //    assemblies.Add(assembly);
            //}

            foreach (var assemb in assemblies)
            {
                foreach (var type in assemb.GetTypes())
                {
                    if (type.IsSubclassOf(typeof(DockWindowViewModel)))
                    {
                        dockWindowViewModelTypes.Add(type);
                    }
                    allTypes.Add(type);
                }
            }

            DockWindowViewModelTypes = dockWindowViewModelTypes.ToArray();
            AllTypes = allTypes.ToArray();
        }
    }
}
